//-----------------------------------------------------------------------
// <copyright file="MembershipFeeDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class MembershipFeeDAOBase
    {
        #region Common methods
        public virtual MembershipFee CreateMembershipFeeFromReader(IDataReader reader)
        {
            MembershipFee item = new MembershipFee();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MembershipFeeID"))) item.MembershipFeeID = (int)reader["MembershipFeeID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("TimeGold"))) item.TimeGold = (int)reader["TimeGold"];
                if (!reader.IsDBNull(reader.GetOrdinal("Other"))) item.Other = (string)reader["Other"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateMembershipFeeFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateMembershipFee methods
            
        public virtual void CreateMembershipFee(MembershipFee membershipFee)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMembershipFeeInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, membershipFee.Title);
                database.AddInParameter(dbCommand, "@TimeGold", DbType.Int32, membershipFee.TimeGold);
                database.AddInParameter(dbCommand, "@Other", DbType.String, membershipFee.Other);
                database.AddOutParameter(dbCommand, "@MembershipFeeID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                membershipFee.MembershipFeeID = (int)database.GetParameterValue(dbCommand, "@MembershipFeeID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateMembershipFeeException, ex);
            }
        }

        #endregion

        #region UpdateMembershipFee methods
        
        public virtual void UpdateMembershipFee(MembershipFee membershipFee)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMembershipFeeUpdate");            
                
                database.AddInParameter(dbCommand, "@MembershipFeeID", DbType.Int32, membershipFee.MembershipFeeID);
                database.AddInParameter(dbCommand, "@Title", DbType.String, membershipFee.Title);
                database.AddInParameter(dbCommand, "@TimeGold", DbType.Int32, membershipFee.TimeGold);
                database.AddInParameter(dbCommand, "@Other", DbType.String, membershipFee.Other);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateMembershipFeeException, ex);
            }
        }
        
        #endregion

        #region DeleteMembershipFee methods
        public virtual void DeleteMembershipFee(int membershipFeeID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMembershipFeeDelete");
                
                database.AddInParameter(dbCommand, "@MembershipFeeID", DbType.Int32, membershipFeeID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteMembershipFeeException, ex);
            }
        }

        #endregion

        #region GetMembershipFee methods
        
        public virtual MembershipFee GetMembershipFee(int membershipFeeID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMembershipFeeGet");
                
                database.AddInParameter(dbCommand, "@MembershipFeeID", DbType.Int32, membershipFeeID);
                
                MembershipFee membershipFee =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        membershipFee = CreateMembershipFeeFromReader(reader);
                        reader.Close();
                    }
                }
                return membershipFee;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMembershipFeeException, ex);
            }
        }
        
        #endregion

        #region GetMembershipFeeList methods
        public virtual MembershipFeeCollection GetMembershipFeeList(MembershipFeeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMembershipFeeGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                MembershipFeeCollection membershipFeeCollection = new MembershipFeeCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        MembershipFee membershipFee = CreateMembershipFeeFromReader(reader);
                        membershipFeeCollection.Add(membershipFee);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return membershipFeeCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMembershipFeeListException, ex);
            }
        }
        
        public virtual MembershipFeeCollection GetMembershipFeeList(MembershipFeeColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetMembershipFeeList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetMembershipFeeList methods
        public virtual MembershipFeeCollection GetMembershipFeeListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMembershipFeeGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                MembershipFeeCollection membershipFeeCollection = new MembershipFeeCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        MembershipFee membershipFee = CreateMembershipFeeFromReader(reader);
                        membershipFeeCollection.Add(membershipFee);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return membershipFeeCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMembershipFeeListException, ex);
            }
        }
        
        public virtual MembershipFeeCollection GetMembershipFeeListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetMembershipFeeListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}